/**
 * Created by ZHOUSH on 12/22/2015.
 */
var app = angular.module('report', []);
app.controller('reportController', function ($scope, $http) {
    var mask = $('#mask');
    $scope.rptTypeList = [
        {
            value: '',
            disp: 'All'
        },
        {
            value: 'ORCL',
            disp: 'Oracle Builder'
        },
        {
            value: 'DSQL',
            disp: 'Direct SQL'
        },
        {
            value: 'JXLS',
            disp: 'JXLS'
        },
        {
            value: 'FODE',
            disp: 'FODE'
        },
        {
            value: 'REPORT',
            disp: 'Report Builder'
        }
    ];
    $scope.rptType = $scope.rptTypeList[0];
    $scope.rptList = [];
    $scope.criteria = {
        'TYPE': $scope.rptTypeList[0]
    };
    $scope.search = function () {
        mask.modal();
        $http.post('/api/rpt/search', {
            'CUST_CODE': $scope.criteria['CUST_CODE'],
            'CUST_NAME': $scope.criteria['CUST_NAME'],
            'RPT_NAME': $scope.criteria['RPT_NAME'],
            'RPT_TITLE': $scope.criteria['RPT_TITLE'],
            'TYPE': $scope.criteria['TYPE'].value,
            meta: {
                schedInfo: $scope.criteria['schedInfo'],
                download: false
            }
        }).success(function (data) {
            mask.modal('hide');
            $scope.rptList = data;
        });
    };
    $scope.download = function (schedInfo) {
        var anchor = angular.element('<a/>');
        anchor.attr({
            href: '/api/rpt/download?criteria=' + encodeURI(JSON.stringify({
                'CUST_CODE': $scope.criteria['CUST_CODE'],
                'CUST_NAME': $scope.criteria['CUST_NAME'],
                'RPT_NAME': $scope.criteria['RPT_NAME'],
                'RPT_TITLE': $scope.criteria['RPT_TITLE'],
                'TYPE': $scope.criteria['TYPE'].value,
                meta: {
                    schedInfo: schedInfo,
                    download: true
                }
            })),
            target: '_blank',
            download: 'Report_Summary' + (schedInfo ? '_Schedule_Info.csv' : '.csv')
        })[0].click();
    };

    var editor = CodeMirror.fromTextArea(document.getElementById('sql-editor'), {
        mode: 'text/x-plsql',
        indentWithTabs: true,
        smartIndent: true,
        lineNumbers: true,
        matchBrackets: true,
        autofocus: true,
        extraKeys: {"Ctrl-Space": "autocomplete"},
        value: 'select * from dual;'
    });
    $scope.sql = function (item) {
        mask.modal();
        $http.post('/api/rpt/sql', {
            key: item['KEY'],
            type: item['TYPE']
        }).success(function (data) {
            mask.modal('hide');
            var val = data[0].sql;
            editor.setValue(typeof val == 'string' ? val : val.join(';'));
        })
    };
    $scope.parse = function () {
        mask.modal();
        var sql = editor.getValue();
        if (sql) {
            $http.post('/api/rpt/parse/plsql', {
                sql: sql
            }).success(function (data) {
                mask.modal('hide');
                console.log(data);
            })
        }
    }
});